/**	
 * player的入口文件
 */
define(['./Audio','./RSLoader','./Layer/main','./Synchronizer','async','eventEmitter'],
	function(Audio,RSLoader,Layer,Synchronizer,async,EventEmitter){
		var Player = function(canvasId,audioId,rsUrl){
			//组件
			this.audio = new Audio();
			this.rsLoader = new RSLoader();
			this.layer = new Layer();
			
			//常用标识
			/**
			 * Player状态
			 * notLoadData		未加载资源
			 * loadingData		正在加载资源
			 * loadDataFaild 	加载资源失败
			 * ready			加载资源成功并准备播放
			 */
			this.status = "notLoadData";
			
			//方法
			/**
			 * 初始化方法
			 */
			this.init = function(){
				//1.先加载初始化资源
				//2.同时载初始化音频
				//3.在加载完资源后载初始化图层
				async.auto({
					initRs: function (callback) {
						this.status = "loadingData";
			        	this.rsLoader.init(rsUrl,function(err){
			        		callback(err);
			        	});
			    	}.bind(this),
				    initAudio: function (callback) {
				        this.audio.init(audioId,rsUrl);
				        callback();
				    }.bind(this),
				    initLayer: ['initRs', function(callback) {
				        this.layer.init(this.rsLoader.imgObj);
				        callback();
				    }.bind(this)]
					}, function(err, results) {
						if(err){
							//加载资源失败
							this.status = "loadDataFaild";
							this.emitter.emit('loadRsError',err);
						}else{
							//加载资源成功
							this.status = "ready";
							this.emitter.emit('loadRsSuccess');
						}
						console.log('初始化完成');
					}.bind(this)
				);
			}
			
			
			//事件初始化
			this.emitter = new EventEmitter(this);
		};
		Player.prototype.on = function(name,callback){
			this.emitter.on(name,callback);
		}
		return Player;
	}
);